#include "Lte4gNetwork.h"

#include <sys/ioctl.h>
#include <unistd.h>

#include "Log.h"
#include "buzzer.h"

Lte4gNetwork::~Lte4gNetwork() {
    //dtor
}


const std::string Lte4gNetwork::SIGNAL_FUNCTION_TIP = "4g signal device file";



std::string& Lte4gNetwork::getRealCardFile(std::string& result){
    result = "/dev/ttyUSB-sig";
   // if (0 > ::access(result.data(), F_OK)){
//        result = "/dev/ttyUSB2";
  //  }

    return result;
}



void Lte4gNetwork::stopHardware() {
    INetwork::stopHardware();

//    mserialPortCommunicator.close();

    ::ioctl(mnetworkCardController.getDeviceFd(), IOREQ_CARDA_1_OFF);
    LOG_DEDUG("Lte4gNetwork::stopHardware");
}

void Lte4gNetwork::startHardware()  {
    usleep(3000*1000);//2500*1000
    LOG_DEDUG("Lte4gNetwork::startHardware");
    ::ioctl(mnetworkCardController.getDeviceFd(), IOREQ_CARDA_1_EN);///mnetworkCardController
    usleep(14500*1000);

    if (running()){
        INetwork::startHardware();
    } else {
        LOG_WARN("Lte4gNetwork::startHardware hardware starting slow,hardware start failure!");

        active();
        /*
        usleep(5000*1000);
        if (running()){
            INetwork::startHardware();
        } else {
            LOG_ERROR("Lte4gNetwork hardware start failure!");
        }
        */
    }
}



void Lte4gNetwork::start() {
  //  getRealCardFile(mcardFile);
    if (running()){
        INetwork::start();
    } else {
        LOG_ERROR("Lte4gNetwork hardware module is not found!");
        active();
    }
}



std::string Lte4gNetwork::getSignalStrength() {
    DeviceCommunicator communicator(mcardFile.data(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
    LOG_DEDUG("begin for getSignalStrength:%s,%d" , communicator.deviceName.data());
    std::string result;
    if (communicator) {
        communicator.write("", REQUEST_SIGNAL, sizeof (REQUEST_SIGNAL) - 1);
        std::string buffer(MAX_BUFFER_SIZE, 0);
        ssize_t n = 0;
        int32_t count = 3;
        do {
            n = ::read(communicator.getDeviceFd(), &buffer[0], buffer.size());
            if (0 < n) {
                LOG_DEDUG(buffer.data());

                std::string::size_type index = buffer.find(SIGNAL_ACK_PREFIX);
                if (std::string::npos != index) {
                    index += sizeof (SIGNAL_ACK_PREFIX);

                    std::string::size_type endIndex = buffer.find(',', index);
                    if (std::string::npos != endIndex){
                        result = buffer.substr(index, endIndex - index);
                    }

                    break;
                }/* else {
                    LOG_ERROR("signal ack error:[%d],[%s]", n, buffer.data());
                }*/
            } else {
                std::string errorMassage{"getSignalStrength read failed"};
                LOG_DEDUG(buildErrorMessage(n, errno, errorMassage).data());
            }


            usleep(15*1000);
        } while (--count);
    } else {
        result = "noModule";
        LOG_ERROR(result.data());
//        mserialPortCommunicator.closeFd(mserialPortCommunicator.getDeviceFd());
//        mserialPortCommunicator.reopen();
//        mserialPortCommunicator.setPort(SIGNAL_FUNCTION_TIP);
    }

    return result;
}








